<template>
  <div class="userlist">
    <el-button type="primary" @click="addRoles=true">添加角色</el-button>
    <user-table ref="updateRoleList" @userList="AllUserList=$event" @OnDistribution="OnDistribution" @OnDelete="OnDelete" @onEdit="OnEdit" />

    <!-- 添加角色对话框 -->
    <el-dialog
      :title="AddRolesForm.id? '编辑角色' : '添加角色'"
      :visible.sync="addRoles"
      width="50%"
      @close="onClose"
    >
      <el-form ref="AddRolesForm" :rules="AddRolesRules" :model="AddRolesForm" label-width="80px">
        <el-form-item prop="roleName" label="角色名称">
          <el-input v-model="AddRolesForm.roleName" />
        </el-form-item>
        <el-form-item prop="roleDesc" label="角色描述">
          <el-input v-model="AddRolesForm.roleDesc" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addRoles = false">取 消</el-button>
        <el-button type="primary" @click="onSave">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 分配权限 -->
    <el-dialog
      title="提示"
      :visible="isShow"
      width="50%"
      @close="CloseTree"
    >
      <el-tree
        ref="tree"
        v-loading="loading"
        :data="data"
        show-checkbox
        node-key="id"
        :default-expand-all="true"
        :default-checked-keys="Selected"
        :props="defaultProps"
      />
      <span slot="footer" class="dialog-footer">
        <el-button @click="isShow = false">取 消</el-button>
        <el-button type="primary" @click="OnTreeSave">确 定</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
import userTable from './components/userTable.vue'
import { AddRolesList, editRolesList, deleteRolesList, SetRolesPower } from '@/api/role.js'
import { getAllPower } from '@/api/power.js'
export default {
  name: 'UserList',
  components: { userTable },
  data() {
    return {
      addRoles: false, // 添加角色对话框
      isShow: false, // 分配权限对话框
      AddRolesForm: {
        roleName: '',
        roleDesc: ''
      }, // 添加角色表单
      AddRolesRules: {
        roleName: [
          { required: true, message: '请输入角色名称', trigger: 'blur' },
          { min: 2, max: 8, message: '长度在 2 到 8 个字符', trigger: 'blur' }
        ],
        roleDesc: [
          { required: true, message: '请输入角色描述', trigger: 'blur' },
          { min: 1, max: 15, message: '长度在 1 到 15 个字符', trigger: 'blur' }
        ]
      },
      // 分配权限
      data: [],
      defaultProps: {
        children: 'children',
        label: 'authName'
      },
      loading: false,
      AllUserList: [],
      Selected: [], // 默认选中的ID
      roleid: '' // 当前用户ID
    }
  },

  created() {

  },
  methods: {
    // 点击了确定
    async onSave() {
      try {
        if (this.AddRolesForm.id) {
        //  编辑
          await editRolesList(this.AddRolesForm)
          this.$message({
            message: '编辑用户成功',
            type: 'success'
          })
          console.log(123)
        } else {
        //  添加
          await AddRolesList(this.AddRolesForm)
          this.$message({
            message: '添加用户成功',
            type: 'success'
          })
          console.log(2)
        }
        this.addRoles = false
        this.$refs.updateRoleList.loadRoleList()
      } catch (err) {
        console.log(err)
      }
    },
    // 关闭对话框
    onClose() {
      this.$refs.AddRolesForm.resetFields()
      this.AddRolesForm = {
        roleName: '',
        roleDesc: ''
      }
    },
    // 编辑
    OnEdit(e) {
      this.addRoles = true
      this.AddRolesForm = JSON.parse(JSON.stringify(e))
    },
    // 删除
    OnDelete(e) {
      this.$confirm('此操作将永久删除该角色, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await deleteRolesList(e)
        this.$refs.updateRoleList.loadRoleList()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 分配权限
    async OnDistribution(e) {
      this.loading = true
      this.isShow = true
      const res = await getAllPower('tree')
      this.data = res.data
      this.loading = false
      this.roleid = e
      const newArr = this.AllUserList.find(item => item.id === e)
      this.fn(newArr)
    },
    // 拿到默认选中的ID
    fn(arr) {
      arr.children.forEach(item => {
        if (item.children) {
          this.fn(item)
        } else {
          this.Selected.push(item.id)
        }
      })
    },
    // 关闭分配权限对话框
    CloseTree() {
      this.isShow = false
      this.Selected = []
    },
    // 分配权限确定按钮
    async OnTreeSave() {
      this.isShow = false
      const rids = this.$refs.tree.getCheckedKeys().concat(this.$refs.tree.getHalfCheckedKeys()).toString()
      await SetRolesPower(this.roleid, rids)
      this.$refs.updateRoleList.loadRoleList()
      this.$message({
        message: '更新成功',
        type: 'success'
      })
    }
  }
}
</script>

<style scoped lang='scss'>
.userlist{
  padding: 20px;
  background-color: #fff;
}
</style>
